const Compiler = require('./compiler')

function webpack(options) {
    //合并参数
    const mergeOptions = _mergeOptions(options)
    
    const compiler = new Compiler(mergeOptions)

    //加载插件
    _loadPlugins(options.plugins, compiler)

    return compiler
}

function _mergeOptions(options) {
    const shellOptions = process.argv.slice(2).reduce((options, argv) => {
        const [key, value] = argv.split('=')
        if(key && value) {
            parseKey = key.slice(2)
            options[parseKey] = value
            return options
        }
    }, {})

    return {...options, ...shellOptions}
}

function _loadPlugins(plugins, compiler) {
    if(plugins && Array.isArray(compiler)) {
        plugins.forEach(plugin => {
            plugin.apply(compiler)
        });
    }
}

module.exports = webpack